const webpack = require('webpack')
const fs = require('fs')
const path = require('path')
const prod = require('./webpack.config')

dir(path.resolve(__dirname, '../src')).then((entry) => {
  webpack(prod(entry), (err, stats) => {
    if (err) throw err;
    process.stdout.write(stats.toString({
          colors: true,
          modules: false,
          children: false,
          chunks: false,
          chunkModules: false
        }) + '\n')
  })
})


// Program Files
function dir(src) {
  return new Promise((res, rej) => {
    fs.readdir(src, (err, fils) => {
      let entry = {};
      fils.forEach((fileName) => {
        if (/(.*)\.js$/.test(fileName)) {
          entry[RegExp.$1] = './src/' + fileName
        }
      });
      res(entry);
    })
  })
}

// https://juejin.im/post/58edcbda44d904005774cfb1?utm_source=gold_browser_extension